﻿package code {
	// class reprsents a bar
	public class Bar extends GameObject {
		public var mass:int; // mass of the bar
		private var area:Number;  // area of the bar
		public var angAlpha:Number;  // angular acceleration
		public var omega:Number;	// angular velocity
		public var pivotPoint:Vertex;	// point to pivot around
		private var pivotFriction:Number = .95;	// amount of friction due to pivot point
		public var topLeftVertex, topRightVertex, bottomLeftVertex, bottomRightVertex:Vertex;
		
		public const ANIMATION_RATE = 100;
		public function Bar(shapeType:String, radius:Number, sides:Number, color:uint, thickness:Number, hollow:Boolean, lineHeight:int, mass:int, pivotPoint:Vertex) {
			super(shapeType, radius, sides, color, thickness, hollow, 0, lineHeight);
			
			this.mass = mass;
			
			angAlpha = 0;
			omega = 0;
			theta = 0;
			this.pivotPoint = pivotPoint;
			init();
		}
		
		public function init(){
			calculateArea();
		}
		public function update(dT:Number){
			omega += angAlpha * (dT / ANIMATION_RATE);		// euler integration of angular velocity.
			omega *= pivotFriction;
			theta += omega * (dT / ANIMATION_RATE);
			
			rotateBar();		// rotates the bar 
			createLinesArray();   // recreates the array of where lines are at 
			updateVertices();  // updates the vertices around the pivot point
		}
		
		// rotates bar about a point based on distance to point
		public function rotateBar(){
			// automatically rotates about 0,0 
			shape.angle = theta;
			shape.rotateVertices();
			shape.graphics.clear();
			shape.renderVertices();
		}
				
		//------------------------------------------------------------------------
		// Rotate a vertex:
		//------------------------------------------------------------------------
		private function rotateVertex(old:Vertex,angle:Number):Vertex {
			var sin:Number = Math.sin(angle);   // rotates vertices of the bar using a rotation matrix
			var cos:Number = Math.cos(angle);
			var Xnew:Number = cos*old.x-sin*old.y;
			var Ynew:Number = sin*old.x+cos*old.y;
			return new Vertex(Xnew,Ynew);
		}
		
		// calculates area of the bar
		public function calculateArea(){
			var tempShape:Rect = (shape as Rect);
			area = tempShape.radius * tempShape.rectHeight;
			trace(area);
		}
		
		// sets up initial vertex position
		// hard coded due to local and global problem
		public function setUpVertices(){
			topLeftVertex = new Vertex(x, y);
			topRightVertex = new Vertex(x + shape.radius, y);
			bottomLeftVertex = new Vertex(x, y + (shape as Rect).rectHeight);
			bottomRightVertex = new Vertex(x + shape.radius, y + (shape as Rect).rectHeight);
		}
		
		// updates the springs attached vertex
		public function updateVertices(){
			// treat center (the pin) as origin of system:
			var tmp1:Vertex = Vertex.sub(topLeftVertex, bottomRightVertex);
			
			// rotate right ball about center:
			var tmp2:Vertex = rotateVertex(tmp1,theta); // use current angle
			
			// translate right ball relative to actual position (the pin):
			//var tmp3:Vertex = Vertex.add(bottomRightVertex, tmp2);
			var tmp3:Vertex = Vertex.add(tmp2,topLeftVertex);

			bottomRightVertex = tmp3;
			

		}
		
		// returns the current intertia of the bar
		public function getInertia(leverArmDisplacement:Number):Number{
			return (2 * mass * Math.pow(leverArmDisplacement, 2));
		}

	}
	
}
